﻿using CSC.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JDGLSys.Forms.Pages.LoginFormPages
{
    /// <summary>
    /// ReadCarPage.xaml 的交互逻辑
    /// </summary>
    public partial class ReadCarPage : Page
    {
        public ReadCarPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //无卡测试
            string carnum = "1234";

            ////有卡，读卡获取卡号
            //string carnum = null;
            //carnum = Car_Read(selectSanqu(2), 2);


            string flag= UsersTManager.selectByCarNum(carnum).ToString();
            if (flag!=null)
            {
                UserForm userForm = new UserForm();
                userForm.Show();
            }
            else
                MessageBox.Show("您还不是我们的用户");
        }
        int dizhi1, dizhi2, dizhi3;
        private int selectSanqu(int sanqu)
        {
            dizhi1 = sanqu * 4;
            dizhi2 = dizhi1 + 1;
            dizhi3 = dizhi2 + 1;
            return sanqu;
        }


        
        private String Car_Read(int sanqu, int dizhi)
        {
            //设备初始化
            //icdev：通讯设备标识符
            int icdev = DcHelper.dc_init(100, 115000);
            String resultData = null;
            if (icdev > 0)
            {
                // 1.寻卡
                long kaxuliehao = 0;
                //寻卡模式--0，单卡模式
                int xunkaMode = 0;

                //寻卡返回结果为0表示成功
                int xunkaR = DcHelper.dc_card(icdev, xunkaMode, ref kaxuliehao);
                if (xunkaR == 0)
                {
                    //2.装载密码
                    //读取到的密码
                    byte[] carPassword = new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
                    //返回0表示成功
                    int zhuangzaiR = DcHelper.dc_load_key(icdev, xunkaMode, sanqu, carPassword);
                    if (zhuangzaiR == 0)
                    {
                        //3.验证密码
                        int yanzhengR = DcHelper.dc_authentication(icdev, xunkaMode, sanqu);
                        if (yanzhengR == 0)
                        {
                            //4.读卡
                            byte[] readRData = new byte[16];
                            int readR = DcHelper.dc_read(icdev, dizhi, readRData);
                            if (readR.Equals(0))
                            {
                                resultData = System.Text.Encoding.Default.GetString(readRData);
                                DcHelper.dc_beep(icdev, 10);
                                MessageBox.Show("读卡完成");
                            }
                            else
                            {
                                MessageBox.Show("卡出现异常，请检查卡是否损坏");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("寻卡失败");
                    }
                }
                else
                {
                    MessageBox.Show("卡出现异常，请检查卡是否损坏");
                }
            }

            return resultData;
            //蜂鸣
            //DcHelper.dc_beep(icdev, 100);
            //测试
            //MessageBox.Show(icdev.ToString());

        }
    }
}
